/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pos.system.database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author shameera
 */
public class DataUtil {

    @Deprecated
    public static List<RowData_Deprecate> getDataList(ResultSet results, String[] columnNames) throws SQLException {
        List<RowData_Deprecate> datas = new ArrayList<RowData_Deprecate>();
        while (results.next()) {
            String stock = "";
            String code = "";
            String description = "";
            double price = 0.0;
            double discount = 0.0;
            int quantity = 0;
            int amount = 0;
            for (String s : columnNames) {
                if (s.equals("STK")) {
                    stock = results.getString("Stock");
                } else if (s.equals("Code")) {
                    code = results.getString("Code");
                } else if (s.equals("Description")) {
                    description = results.getString("Description");
                } else if (s.equals("Price")) {
                    price = Double.parseDouble(results.getString("Price"));
                } else if (s.equals("Discount")) {
                    discount = Double.parseDouble(results.getString("Discount"));
                } else if (s.equals("Qty")) {
                    quantity = Integer.parseInt(results.getString("Quantity"));
                } else if (s.equals("Amount")) {
                    amount = Integer.parseInt(results.getString("Amount"));
                }
            }
            RowData_Deprecate r = new RowData_Deprecate(stock, code, description, price, discount, quantity, amount);
            datas.add(r);
        }
        return datas;
    }

    public static List<RowData_Deprecate> getRowDataList(ResultSet results, Column[] columns) throws SQLException {
        List<RowData_Deprecate> datas = new ArrayList<RowData_Deprecate>();
        while (results.next()) {
            for (Column col : columns) {
            }
        }
        return datas;
    }

    public static synchronized List<StockRowData> getCodeSearchResult(ResultSet results, String s) throws SQLException {
        List<StockRowData> codeSearchResults = new ArrayList<StockRowData>();
        if (results != null) {
            if (results.first()) {
                do {
                    String code = results.getString("Code");
                    if (code.indexOf(s) == 0) {
                        StockRowData r = new StockRowData();
                        r.setCode(code);
                        r.setItemName(results.getString("Item Name"));
                        r.setSeller(results.getString("Seller"));
                        r.setQty(results.getString("Qty"));
                        r.setUnit_price(results.getString("Price"));
                        codeSearchResults.add(r);
                    }
                } while (results.next());
            }
        }
        return codeSearchResults;
    }

    public static synchronized List<StockRowData> getNameSearchResult(ResultSet results, String s) throws SQLException {
        List<StockRowData> nameSearchResults = new ArrayList<StockRowData>();
        if (results != null) {
            if(results.first()){
              do{
                  String itemName = results.getString("Item Name");
                if (itemName.indexOf(s) >= 0) {
                    StockRowData r = new StockRowData();
                    r.setCode(results.getString("Code"));
                    r.setItemName(itemName);
                    r.setSeller(results.getString("Seller"));
                    r.setQty(results.getString("Qty"));
                    r.setUnit_price(results.getString("Price"));
                    nameSearchResults.add(r);
                }
              }while(results.next());  
            }
        }
        return nameSearchResults;
    }
    
    
}
